/**
 * Created by Administrator on 2015/12/3.
 */
"use strict";
var F = require('../../../common/function');
var order_F = require('./order_common');
var _o2oServiceApi = require('../../../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;
var _ = require('underscore');
/**
 * 获取商家的订单列表
 * @param queryObj
 */
exports.order_list = function *(queryObj){
    queryObj.act =2;
    queryObj.vid =queryObj.id;
    delete queryObj.id;

    //按手机号查询
    if(!!queryObj.mobile){
        let userInfo =yield  F.getUserByMobile(queryObj.mobile);
        if(_.isEmpty(userInfo)){
            return F.returnMsg('400','手机号不存在！',1);
        }
        queryObj.uid = userInfo.uid;
        delete queryObj.mobile;
    }

    //按订单id查询
    if(!!queryObj.orderid){
        queryObj.orderid = parseInt(queryObj.orderid);
    }
    //按订单编号查询
    if(!!queryObj.order_code){
        queryObj.order_code =_.str.trim(queryObj.order_code);
    }

    //按项目名查询
    if(!!queryObj.wareName){
        //按商品名和商家id查找商品id
        let wareResult = yield o2oServiceApi.post('ware','list',1,{act:3,name:queryObj.wareName,vid:queryObj.vid});
        if(wareResult.status ==400 || wareResult['result'].length==0){
            return F.returnMsg('400','商品不存在！',1);
        }
        queryObj.wareid = wareResult['result'][0]['id'];
        delete  queryObj.wareName;
    }

    let orderResult = yield o2oServiceApi.post('order','list',1,queryObj);
    if(orderResult.status == 400 || orderResult['result']['error']==1){
        return F.returnMsg(300, orderResult['message']['msg'], 1);
    }else{
        if (orderResult['result'] && orderResult['result'].length ==0){
            return F.returnMsg(300, '已经没有数据', 1, orderResult['result']);
        }
        yield order_F.addUserInfo(orderResult);
        return F.returnMsg(200, '查找成功', 1, orderResult['result']);
    }
}

